package org.jeecg.modules.user.member.controller;

import com.alipay.api.response.AlipayUserAgreementQueryResponse;
import com.alipay.api.response.AlipayUserAgreementUnsignResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.user.member.service.PayService;
import org.jeecg.modules.user.pay.dto.AlipayReq;
import org.jeecg.modules.user.pay.dto.PayReq;
import org.jeecg.modules.user.withhold.entity.BikeMemberWithhold;
import org.jeecg.modules.user.withhold.service.IBikeMemberWithholdService;
import org.jeecg.modules.utils.AliPayKit;
import org.jeecg.modules.utils.ItzResult;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
@Slf4j
@Validated
@RestController
@CrossOrigin
@RequestMapping("/alipay")
@Api(value = "支付", description = "支付", tags = {"支付"})
public class PaymentController {


    @Autowired
    private IBikeMemberWithholdService  bikeMemberWithholdService;
    @Autowired
    private PayService payService;

    //@ApiOperation(value = "充值", notes = "充值")
    //@PostMapping("/recharge")
    //public Map<String, String> recharge(@RequestBody AlipayReq alipayReq, HttpServletRequest request) {
    //    Map<String, String> map = payService.recharge(alipayReq, request);
    //    return AliPayKit.pay(map);
    //}

    @ApiOperation(value = "充值", notes = "充值")
    @PostMapping("/recharge")
    public ItzResult recharge(@RequestBody AlipayReq alipayReq, HttpServletRequest request) {
        return ItzResult.ok("1","充值功能暂未开放，敬请期待",null);
    }
    @ApiOperation(value = "付款", notes = "付款")
    @PostMapping("/reCar")
    public Map<String, String> recharge(@RequestBody PayReq payReq, HttpServletRequest request) {
       return payService.recharge(payReq,request);
    }


    @Transactional
    @PostMapping("/buyVipByAlipay")
    public String settleInByAlipay(HttpServletRequest request) {
     return payService.settleInByAlipay(request);
    }


    @Transactional
    @PostMapping("/payment")
    public String payment(HttpServletRequest request) {
        log.info("支付宝回调1");
        return payService.payment(request);

    }
    @Transactional
    @PostMapping("/mixedPay")
    public String mixedPay(HttpServletRequest request) {
        log.info("支付宝回调2");
        return payService.mixedPay(request);

    }

    @ApiOperation(value = "支付宝签约", notes = "支付宝签约")
    @PostMapping("/withhold")
    public Map<String, String> withhold(HttpServletRequest request){
        String accessToken = request.getHeader("X-Access-Token");
        // 解密获得username，用于和数据库进行对比
        String username = JwtUtil.getUsername(accessToken);
        return AliPayKit.withhold(username);
    }


    @PostMapping("/withholdingResults")
    public String withholdingResults(HttpServletRequest request)  {
        AlipayUserAgreementQueryResponse cx = AliPayKit.cx(request);
        int i = bikeMemberWithholdService.isnertWithhold(cx);
        return "success";
    }

    @ApiOperation(value = "支付宝解约", notes = "支付宝解约")
    @PostMapping("/rescind")
    public ItzResult rescind(HttpServletRequest request){
        String accessToken = request.getHeader("X-Access-Token");
        // 解密获得username，用于和数据库进行对比
        String username = JwtUtil.getUsername(accessToken);

        BikeMemberWithhold withhold = bikeMemberWithholdService.getOne(new QueryWrapper<BikeMemberWithhold>().eq("user_id", username).eq("status", "NORMAL"));

        Map map = new HashMap();
        map.put("alipayLogonId",withhold.getAlipayLogonId());
        map.put("personalProductCode",withhold.getPersonalProductCode());
        map.put("signScene",withhold.getSignScene());
        map.put("externalAgreementNo",withhold.getExternalAgreementNo());
        map.put("thirdPartyType",withhold.getThirdPartyType());
        map.put("agreementNo",withhold.getAgreementNo());

        AlipayUserAgreementUnsignResponse rescind = AliPayKit.rescind(map);
        if (rescind.getCode().equals("10000")){
            //解约成功
            BikeMemberWithhold bikeMemberWithhold = new BikeMemberWithhold();
            bikeMemberWithhold.setId(withhold.getId());
            bikeMemberWithhold.setStatus("STOP");
            boolean b = bikeMemberWithholdService.updateById(bikeMemberWithhold);
        }
        return ItzResult.ok("1","解约成功",null);
    }


    }
